<?php
if (!defined('BASEPATH'))
	exit ('No direct script access allowed');

class Edit extends CI_Controller {
	var $data;
	function __construct() {
		parent :: __construct();
		if ($this->session->userdata('userid') == FALSE) {
			Header("Location:" . site_url('welcome'));
			exit ();
		}
		$this->data['username'] = $this->session->userdata('username');
	}
	function index() {
		parse_str($_SERVER['QUERY_STRING'], $_GET);
		$id = isset ($_GET['id']) ? $_GET['id'] : '';
		$this->load->library('ckeditor');
		$this->load->library('ckFinder');
		$this->ckeditor = new CKEditor();
		$this->ckeditor->basePath = '/js/ckeditor/';
		$this->ckeditor->config['toolbar'] = 'Full';
		$this->ckeditor->config['language'] = 'zh-cn'; // $this -> ckeditor -> config[''] 添加一些你需要的配置详细参数自己查一下
		CKFinder :: SetupCKEditor($this->ckeditor, '/js/ckfinder/');
		$this->load->model('infos');
		$info = $this->infos->select_info($id);
		$this->data = $info;
		$this->load->view('admin/edit', $this->data);
	}
	function updates() {
		if (isset ($_POST['mailtpl_content'])) {
			$this->data['mailtpl_content'] = $_POST['mailtpl_content'];
			$this->data['title'] = $_POST['title'];
			$this->data['id'] = $_POST['id'];
			$this->data['writer'] = $_POST['writer'];
			$this->data['class_id'] = $_POST['class_id'];
			$this->data['description'] = $_POST['description'];

			$str= $_POST['mailtpl_content'];//文章内容（包括图片）
			if(preg_match_all("/<img .*\/>/isU",$str,$ereg))
			{
			preg_match_all("/<img .*\/>/isU",$str,$ereg);//把图片的<img 整个都获取出来了
			$img  = $ereg[0][0];//图片
			preg_match_all ('|src="(.*)"|isU', $img, $img1);
			$img_path = $img1[1][0];//获取第一张图片路径
			$this->data['pic'] = $img_path;
			}
			$this->load->model('infos');
			$this->infos->update_info('info', 'content', $this->data['mailtpl_content'], $this->data['id']);
			$this->infos->update_info('info', 'description', $this->data['description'], $this->data['id']);
			$this->infos->update_info('info', 'writer', $this->data['writer'], $this->data['id']);
			$this->infos->update_info('info', 'title', $this->data['title'], $this->data['id']);
			$this->infos->update_info('info', 'class_id', $this->data['class_id'], $this->data['id']);
			$this->infos->update_info('info', 'pic', $this->data['pic'], $this->data['id']);
			$this->infos->update_info('info', 'editdate', time(), $this->data['id']);
			$this->message['message_info'] = '信息更新成功！';
			$this->message['message_url'] = site_url('admin/edit?id=' . $this->data['id']);
			$this->load->view('admin/message', $this->message);
		} else {
			$this->load->view('welcome', $this->data);
		}
	}
	function delete() {
		parse_str($_SERVER['QUERY_STRING'], $_GET);
		$id = isset ($_GET['id']) ? $_GET['id'] : '';
		$this->load->model('infos');
		$this->infos->delInfo($id);
		$this->message['message_info'] = '删除成功！';
			$this->message['message_url'] = site_url('admin/info');
			$this->load->view('admin/message', $this->message);
	}
}